#include "message.h"

Message::Message()
    :m_type(MsgType::HEART)
    ,m_message("HEART")////心跳消息，消息体应该固定为HEART
{
}

Message::Message( const QString &msg):
    m_type(MsgType::MSG),m_message(msg)
{
}

Message::Message(QByteArray & array )
{
    QBuffer  buf(&array);
     //用QBuffer将QByteArray当做文件来打开并读取数据
    buf.open(QIODevice::ReadOnly);
    buf.read((char *)&m_type, sizeof(int) );
    m_message = buf.readAll();
    buf.close();
}

QByteArray  Message::toByteArray()
{
    QByteArray  array;
    //用QBuffer将QByteArray当做文件来打开并写入数据
    QBuffer  buf(&array);
    buf.open(QIODevice::WriteOnly);
    buf.write((char *)&m_type, sizeof(int) );
    buf.write( m_message.toUtf8(), m_message.toUtf8().size());
    buf.close();

    return array;
}
